<?php
class ModelBlogComment extends Model {
	public function addComment($data) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "blog_comments SET 
			author_name = '" . $this->db->escape($data['author_name']) . "', 
			author_email = '" . $this->db->escape($data['author_email']) . "', 
			blog_post_id = '" . $this->db->escape($data['blog_post_id']) . "', 
			content = '" . $this->db->escape(strip_tags($data['content'])) . "', 
			rating = '" . (int)$data['rating'] . "', 
			approved = '" . (int)$data['approved'] . "',
			published = '" . (int)$data['published'] . "',
			modified = NOW(), 
			created = NOW()");

		$this->cache->delete('blog_comment');
	}
	
	public function editComment($id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "blog_comments SET 
			author_name = '" . $this->db->escape($data['author_name']) . "', 
			author_email = '" . $this->db->escape($data['author_email']) . "', 
			blog_post_id = '" . $this->db->escape($data['blog_post_id']) . "', 
			content = '" . $this->db->escape(strip_tags($data['content'])) . "', 
			rating = '" . (int)$data['rating'] . "', 
			approved = '" . (int)$data['approved'] . "', 
			published = '" . (int)$data['published'] . "',
			modified = NOW() 
			WHERE id = '" . (int)$id . "'");
	
		$this->cache->delete('blog_comment');
	}
	
	public function publishComment($id) {
		$this->db->query("UPDATE blog_comments SET published='1' WHERE id = '" . (int)$id . "'");
		$this->cache->delete('blog_comment');
	}	
	
	public function unPublishComment($id) {
		$this->db->query("UPDATE blog_comments SET published='0' WHERE id = '" . (int)$id . "'");

		$this->cache->delete('blog_comment');
	}
	
	public function deleteComment($id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "blog_comments WHERE id = '" . (int)$id . "'");
		
		$this->cache->delete('blog_comment');
	}
	
	public function getComment($id) {
		$query = $this->db->query("SELECT 

		bc.id as id, 
		bpd.title as title, 
		bc.blog_post_id as blog_post_id, 
		bc.author_name as author_name, 
		bc.author_email as author_email, 
		bc.author_ip as author_ip, 
		bc.content as content,  
		bc.approved as approved,
		bc.published as published,
		bc.created as created,
		bc.rating as rating 
		
		FROM " . DB_PREFIX . "blog_comments bc LEFT JOIN " . DB_PREFIX . "blog_posts_description bpd ON(bpd.blog_post_id = bc.blog_post_id) WHERE bc.id = '" . (int)$id . "'");
		
		return $query->row;
	}

	public function getComments($data = array()) {
		$sql = "SELECT 
					bc.id as id, 
					bpd.title as title, 
					bc.blog_post_id as blog_post_id, 
					bc.author_name as author_name, 
					bc.author_email as author_email, 
					bc.author_ip as author_ip, 
					bc.content as content,  
					bc.approved as approved,
					bc.published as published,
					bc.created as created,
					bc.rating as rating 
				FROM 
					" . DB_PREFIX . "blog_comments bc 
					LEFT JOIN " . DB_PREFIX . "blog_posts_description bpd ON (bc.blog_post_id = bpd.blog_post_id) WHERE 1";


		if (!empty($data['filter_title'])) {
			$sql .= "  AND LCASE(bpd.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}
		
		if (!empty($data['filter_author_name'])) {
			$sql .= "  AND LCASE(bc.author_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_author_name'])) . "%'";
		}

		if (isset($data['filter_rating']) && !is_null($data['filter_rating'])) {
			$sql .= " AND bc.rating = '" . (int)$data['filter_rating'] . "'";
		}
		
		if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
			$sql .= " AND bc.approved = '" . (int)$data['filter_approved'] . "'";
		}		
		
		if (isset($data['filter_published']) && !is_null($data['filter_published'])) {
			$sql .= " AND bc.published = '" . (int)$data['filter_published'] . "'";
		}
		
		$sort_data = array(
			'bpd.title',
			'bc.author_name',
			'bc.rating',
			'bc.approved',
			'bc.created',
			'bc.published'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY created";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}																																		  
		$query = $this->db->query($sql);																														
		return $query->rows;	
	} 
	
	public function getTotalComments($data) {
		$sql = "SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_comments bc LEFT JOIN " . DB_PREFIX . "blog_posts_description bpd ON (bc.blog_post_id = bpd.blog_post_id) WHERE 1";
		
		
		if (!empty($data['filter_title'])) {
			$sql .= "  AND LCASE(bpd.title) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_title'])) . "%'";
		}
		
		if (!empty($data['filter_author_name'])) {
			$sql .= "  AND LCASE(bc.author_name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_author_name'])) . "%'";
		}

		if (isset($data['filter_rating']) && !is_null($data['filter_rating'])) {
			$sql .= " AND bc.rating = '" . (int)$data['filter_rating'] . "'";
		}
		
		if (isset($data['filter_approved']) && !is_null($data['filter_approved'])) {
			$sql .= " AND bc.approved = '" . (int)$data['filter_approved'] . "'";
		}		
		
		if (isset($data['filter_published']) && !is_null($data['filter_published'])) {
			$sql .= " AND bc.published = '" . (int)$data['filter_published'] . "'";
		}
		
		$query = $this->db->query($sql);
		
		return $query->row['total'];
	}
	
	public function getTotalCommentsAwaitingApproval() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "blog_comments WHERE status = '0'");
		
		return $query->row['total'];
	}	
}
?>